﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FighttheLandOwner.GameCore
{
    class CardManager
    {
        public List<Card> Cards { get; set; }

        public CardManager()
        {
            Cards = new List<Card>();
            GenerateCards();
            WashCards();
        }

        /// <summary>
        /// 发牌
        /// </summary>
        /// <returns>四个牌堆，3个玩家，一个底牌</returns>
        public HeappedCards HandOutCards()
        {    
            var result = new HeappedCards();
            int j = 0;
            for (int i = 0; i < 17; i++) 
            {
                result.Player1.Add(Cards[j++]);
                result.Player2.Add(Cards[j++]);
                result.Player3.Add(Cards[j++]);
            }
            result.LeftCards.Add(Cards[j++]);
            result.LeftCards.Add(Cards[j++]);
            result.LeftCards.Add(Cards[j++]);
            return result;
        }

        /// <summary>
        /// 产生一副牌
        /// </summary>
        private void GenerateCards()
        {
            List<CardType> cardTypes = new List<CardType>();
            List<CardValue> values = new List<CardValue>();
            cardTypes.Add(CardType.Clubs);cardTypes.Add(CardType.Diamond);cardTypes.Add(CardType.Heart);cardTypes.Add(CardType.Spade);
            values.Add(CardValue.A); values.Add(CardValue.Eight); values.Add(CardValue.Five); values.Add(CardValue.Four); values.Add(CardValue.J);
            values.Add(CardValue.K); values.Add(CardValue.Nine); values.Add(CardValue.Q); values.Add(CardValue.Seven);
            values.Add(CardValue.Six); values.Add(CardValue.Ten); values.Add(CardValue.Three); values.Add(CardValue.Two);
                foreach(var t in cardTypes)
                {
                    foreach(var v in values)
                    {
                        Cards.Add(new Card(t, v));
                    }
                }
            Cards.Add(new Card(CardType.Heart, CardValue.Joker));
            Cards.Add(new Card(CardType.Spade, CardValue.Joker));
        }

        /// <summary>
        /// 洗牌
        /// </summary>
        private void WashCards()
        {
            var washedcards = new List<Card>();
            Random r = new Random();
            while(Cards.Count>0)
            {
                var i = r.Next(0, Cards.Count);
                washedcards.Add(Cards[i]);
                Cards.RemoveAt(i);
            }
            Cards.AddRange(washedcards);
        }

        /// <summary>
        /// 对牌进行按照从左到右递减排序
        /// </summary>
        /// <param name="cards"></param>
        /// <returns></returns>
        public static List<Card> Arange(List<Card> cards)
        {
            cards.Sort(new SortCard());
            return cards;
        }

        /// <summary>
        /// 排序所用比较器
        /// </summary>
        public class SortCard : IComparer<Card>
        {
            public int Compare(Card x, Card y)
            {
                if (x.ToInt() > y.ToInt()) return -1;
                else if (x.ToInt() < y.ToInt()) return 1;
                else
                {
                    if (x.Color == CardColor.Red && y.Color == CardColor.Black)
                        return -1;
                    else if (x.Color == CardColor.Black && y.Color == CardColor.Red)
                        return 1;
                    else
                    {
                        if (x.Color == CardColor.Red)
                        {
                            if (x.Type == CardType.Heart)
                                return -1;
                            else return 1;
                        }
                        else 
                        {
                            if (x.Type == CardType.Spade)
                                return -1;
                            else return 1;
                        }
                    }
                }
            }
        }

    }

    /// <summary>
    /// 牌堆
    /// </summary>
    class HeappedCards
    {
        public List<Card> Player1 { get; set; }
        public List<Card> Player2 { get; set; }
        public List<Card> Player3 { get; set; }
        /// <summary>
        /// 底牌
        /// </summary>
        public List<Card> LeftCards { get; set; }

        public HeappedCards()
        {
            Player1 = new List<Card>();
            Player2 = new List<Card>();
            Player3 = new List<Card>();
            LeftCards = new List<Card>();
        }
    }
}
